package com.jingdianjichi.project.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * ClassName:DruidEncryptUtils
 * Package:com.jingdianjichi.subject.infra.basic.utils
 * Description:
 *
 * @Author fst
 * @Create 2024/9/26 11:00
 * @Version 1.0
 * 数据库加密
 */
public class DruidEncryptUtils
{
    public static String publickey;
    public static String privatekey;

    static
    {
        try {
            String[] KeyPair = ConfigTools.genKeyPair(512);
            privatekey=KeyPair[0];
            System.out.println("privatekey: "+privatekey);
            publickey= KeyPair[1];
            System.out.println("publickey: "+publickey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }

    public static String  encrypt(String plainText)
    {
        try {
            String encrypt = ConfigTools.encrypt(privatekey,plainText);
            System.out.println("encrypt: "+encrypt);
            return encrypt;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
    public  static  String  decrypt(String plainText)
    {
        try {
            String decrypt = ConfigTools.decrypt(publickey, plainText);
            System.out.println("decrypt："+decrypt);
            return decrypt;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        decrypt(encrypt("Wing1Q2W#E"));
    }
}
